package ro.unibuc.nautilus.controllers;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Required;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.bind.ServletRequestUtils;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.SimpleFormController;

import ro.unibuc.nautilus.controllers.editors.DatePropertyEditor;
import ro.unibuc.nautilus.controllers.editors.HarbourPropertyEditor;
import ro.unibuc.nautilus.domain.Cargo;
import ro.unibuc.nautilus.domain.CruiseShip;
import ro.unibuc.nautilus.domain.Harbour;
import ro.unibuc.nautilus.domain.Ship;
import ro.unibuc.nautilus.domain.ShipHolder;
import ro.unibuc.nautilus.domain.Tanker;
import ro.unibuc.nautilus.service.HarboursService;
import ro.unibuc.nautilus.service.ShipsService;

public class EditShipController extends SimpleFormController {
	private HarboursService harboursService;
	private ShipsService shipsService;

	public EditShipController() {
	}
	
	@Override
	protected Object formBackingObject(HttpServletRequest request)
			throws Exception {
		
		ShipHolder holder = new ShipHolder();
		long shipId = ServletRequestUtils.getLongParameter(request, "shipId", -1);
		
		// Assume create new if argument is missing
		if (shipId == -1) {
			/// cruise by default
			Ship newShip = new CruiseShip();
			
			String shipType = ServletRequestUtils.getStringParameter(request, "shipType");
			
			if (Ship.CARGO.equals(shipId)) {
				newShip = new Cargo();
			} else if (Ship.TANKER.equals(shipType)) {
				newShip = new Tanker();
			}
			
			holder.ship = newShip;
			return holder;
		}
		
		holder.ship = shipsService.findShip(shipId); 
		// for edit mode should not allow change type
		return holder;
	}
	
	@Override
	protected Map referenceData(HttpServletRequest request) throws Exception {
		Map<Object, Object> model = new HashMap<Object, Object>();
		ArrayList<String> shipTypes = new ArrayList<String>();
		shipTypes.add(Ship.CRUISE_SHIP);
		shipTypes.add(Ship.CARGO);
		shipTypes.add(Ship.TANKER);
		
		model.put("harbours", harboursService.findAll());
		model.put("shipTypes", shipTypes);
		
		return model;
	}
	
	protected void initBinder(HttpServletRequest request,
			ServletRequestDataBinder binder) {

		HarbourPropertyEditor hpe = new HarbourPropertyEditor();
		hpe.setHarboursService(harboursService);

		binder.registerCustomEditor(Harbour.class, hpe);
		binder.registerCustomEditor(Date.class, new DatePropertyEditor());
		
//		binder.setDisallowedFields(new String[] {"shipType"});
//		 
//    	Ship ship = ((ShipHolder) binder.getTarget()).ship;
//    	
//    	ship.setShipType(request.getParameter("shipType"));
	}
	 
	
	@Override
	protected ModelAndView onSubmit(Object command) throws Exception {
		Ship ship = ((ShipHolder) command).ship;
		shipsService.createOrUpdate(ship);
		return new ModelAndView(getSuccessView());
	}

	@Required
	public void setHarboursService(HarboursService harboursService) {
		this.harboursService = harboursService;
	}

	@Required
	public void setShipsService(ShipsService shipsService) {
		this.shipsService = shipsService;
	}
	
}
